package ${base_pack}.service.impl;

import ${bean_pack};
import cn.jiayen.mvc.error.CustomParameterizedException;
import cn.jiayen.mvc.page.PagerVO;
import ${base_pack}.repository.${name}Repository;
import ${base_pack}.service.${name}Service;
import ${base_pack}.views.dto.${name}DTO;
import ${base_pack}.views.vo.${name}VO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import cn.jiayen.comm.util.CustomerStringUtils;
/**
 * Created by jiayen on 2018/5/5.
 */
@Service
@Slf4j
public class ${name}ServiceImpl implements ${name}Service {

    @Autowired
    private ${name}Repository ${m_name}Repository;

    @Override
    public ${name}VO add(${name}DTO ${m_name}) {
        ${name} bean = new ${name}();
        BeanUtils.copyProperties(${m_name},bean);
        ${m_name}Repository.save(bean);
        ${name}VO ${m_name}VO = new ${name}VO();
        BeanUtils.copyProperties(bean,${m_name}VO);
        return ${m_name}VO;
    }

    @Transactional(rollbackOn = Exception.class)
    @Override
    public void deleteById(String ids) {
        if(StringUtils.isEmpty(ids)){
        throw  new CustomParameterizedException("请输入要操作的id");
        }
        try {
             ${m_name}Repository.deleteByIds(CustomerStringUtils.strToList(ids));
        }catch (Exception e){
             log.error("删除失败:",e);
            throw  new CustomParameterizedException("删除失败，请稍后再试!");
        }
    }

    @Override
    public ${name}VO update(${name}DTO ${m_name}) {
        Integer id = ${m_name}.getId();
        if(StringUtils.isEmpty(id)){
            throw  new CustomParameterizedException("请输入正确更新的数据");
        }
        ${name} bean = ${m_name}Repository.getOne(id);
        BeanUtils.copyProperties(${m_name},bean);
        ${m_name}Repository.save(bean);
        ${name}VO vo = new ${name}VO();
        BeanUtils.copyProperties(bean,vo);
        return vo;
    }

    @Override
    public ${name}VO getById(Integer id) {
        if(StringUtils.isEmpty(id)){
            throw  new CustomParameterizedException("请输入正确的id");
        }
        try {
            ${name} bean = ${m_name}Repository.getOne(id);
            ${name}VO vo = new ${name}VO();
            BeanUtils.copyProperties(bean,vo);
            return vo;
        }catch (Exception e){
            log.error("该条数据不存在:",e);
            throw  new CustomParameterizedException("该条数据不存在!");
        }

    }

    @Override
    public PagerVO<${name}VO> get${name}List(Integer page, Integer pageSize) {
        //默认按照id倒序排序
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        if (page < 1) {
            page = 1;
        }
        Pageable pageable =  PageRequest.of(page-1, pageSize, sort);
        Page<${name}> pageContent = ${m_name}Repository.findAll(pageable);

        List<${name}> list = pageContent.getContent();
        List<${name}VO> ${m_name}VOList = new ArrayList<>();
        list.stream().forEach(${m_name} -> {
            ${name}VO vo = new ${name}VO();
            BeanUtils.copyProperties(${m_name},vo);
            ${m_name}VOList.add(vo);
        });
        PagerVO<${name}VO> pagerVO =new PagerVO<>(page,pageSize,pageContent.getTotalElements());
        pagerVO.setRows(${m_name}VOList);
        return pagerVO;
    }
}
